import 'package:flutter/material.dart';
import 'package:flutter_widget/src/widget/effect/border_widget.dart';

///圆角按键
// ignore: must_be_immutable
class RoundButton extends StatelessWidget {
  final Color? backgroundColor; //背景色
  final Widget? child; //子控件
  final VoidCallback? onPressed; //点击事件
  final double? radius; //圆角大小
  final double? height; //高度
  final double? width; //宽度
  final Color? borderColor; //边框颜色
  final double borderWidth; //边框
  final bool touchLimit; //是否点击限制
  final int touchLimitSecond; //时长
  final Color? disabledColor; //不能点击时的颜色（onPressed == null）
  final Color? disabledTextColor;

  RoundButton({
    Key? key,
    this.backgroundColor,
    this.child,
    this.onPressed,
    this.radius,
    this.height,
    this.width,
    this.borderColor,
    this.borderWidth = 0,
    this.touchLimit = true,
    this.touchLimitSecond = 1,
    this.disabledTextColor,
    this.disabledColor,
  }) : super(key: key);

  bool _isTap = false;

  void _onTapAction() {
    //1秒点一次
    if (_isTap == true && touchLimit == true) {
      return;
    }
    _isTap = true;
    if (onPressed != null) {
      onPressed!();
    }
    Future.delayed(Duration(seconds: touchLimitSecond)).then((_) {
      _isTap = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    var button = TextButton(
      style: ButtonStyle(
        //圆角
        shape: MaterialStateProperty.all(
          RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(radius ?? height! / 2),
          ),
        ),
        //边框
        // side: MaterialStateProperty.all(
        //   BorderSide(
        //     color: Colors.red,
        //     width: 0.67,
        //   ),
        // ),
        backgroundColor: MaterialStateProperty.all(backgroundColor ?? Colors.transparent),
        minimumSize: MaterialStateProperty.all(const Size(1, 1)),
        tapTargetSize: MaterialTapTargetSize.shrinkWrap,
        padding: MaterialStateProperty.all(EdgeInsets.zero),
      ),
      onPressed: onPressed != null ? _onTapAction : null,
      child: child!,
    );
    if (borderWidth > 0) {
      return BorderWidget(
        borderColor: borderColor,
        borderWidth: borderWidth,
        width: width,
        height: height,
        radius: radius ?? height! / 2,
        // isInk: true,
        backgroundColor: backgroundColor,
        child: button,
      );
    }
    return SizedBox(
      width: width,
      height: height,
      child: button,
    );
  }
}
